<?php
require_once(APPPATH . '/controllers/super_admin' . EXT);

class Profile extends Super_admin {

        protected $c = 'Profile';

	function __construct()
	{
		parent::__construct();
		
		$this->load->model(array(
			'User_model',
		));
		
		$this->load->library('email');
	}
	
	function index()
	{
		$this->view();
	}
	/**
	 * edit
	 * 
	 * Load edit employee form
	 * 
	 * @access public
	 * @param void
	 * @return void
	 */
	function edit()
	{
            $select = 'u.*';

            $data = $this->User_model->get_record(array('select' => $select,
                        'where' => 'u.user_id = ' . _session('user_id')
                    ));

            $data['title'] = 'Edit Profile';

            $this->load->view('profile/form', $data);
	}
	
	/**
	 * submit
	 * 
	 * Process saving of employee
	 * 
	 * @access public
	 * @param boolean
	 * @return void
	 */
	function submit()
	{
            $response = FALSE;
            $user_id = _session('user_id');

            $data = array(
                'email' => trim($this->input->post('email', TRUE)),
                'contact_no' => trim($this->input->post('contact_no', TRUE)),
                'contact_person' => $this->input->post('contact_person', TRUE),
                'address' => trim($this->input->post('address', TRUE))
            );

            $where = 'user_id =' . $user_id;

            if (!$user_id = $this->User_model->_update($where, $data)) {
                $response = TRUE;
                _error(sprintf(lang('cms_edit_error'),$this->c), FALSE);
                echo json_encode($response);
                return;
            }

            $response = TRUE;
            _success(sprintf(lang('cms_edit_success'),$this->c), FALSE);
            echo json_encode($response);
	}
	/**
	 * view
	 * 
	 * Load view employee page
	 * 
	 * @access public
	 * @param integer
	 * @return void
	 */
	function view()
	{

            $select = 'u.*';

            $data = $this->User_model->get_record(array('select' => $select,
                        'where' => 'u.user_id = ' . _session('user_id')
                    ));

            $this->load->view('profile/display', $data);
	}
	/**
	 * change_password
	 * 
	 * Load change password page
	 * 
	 * @access public
	 * @param integer
	 * @return void
	 */	
	function change_password()
	{
            $select = 'u.*';

            $data = $this->User_model->get_record(array('select' => $select,
                        'where' => 'u.user_id = ' . _session('user_id')
                    ));
		
            $this->load->view('profile/changepassword', $data);
	}
	/**
	 * change_password
	 * 
	 * For check old password
	 * 
	 * @access public
	 * @param void
	 * @return bool
	 */		
	function check_password()
	{
		$password = $this->input->post('password');
		$user_id = $this->input->post('user_id');
		
		$where = array('password' => md5($password));
		
		if ( ! empty($user_id))
		{
			$where['user_id ='] = $user_id;
		}

		$valid = ($this->User_model->exists($where, USERS)) ? TRUE : FALSE;
		echo json_encode($valid);
	}
	/**
	 * submit_change_password
	 * 
	 * Processes change password form
	 * 
	 * @access public
	 * @param void
	 * @return void
	 */
	function submit_change_password()
	{
		$password = $this->input->post('new_password', TRUE);
		
		$data = array(
			'password' => md5($password)
		);

		if ( ! $this->User_model->_update('user_id = '. _session('user_id'), $data))
		{
                    $response = TRUE;
                    _error(sprintf(lang('cms_error'),$this->c), FALSE);
                    echo json_encode($response);
		}

                $response = TRUE;
                _success(sprintf(lang('cms_success'),$this->c), FALSE);
                echo json_encode($response);
        }
        /**
         * unique
         *
         * Check for unique record.
         *
         * @access public
         * @param void
         * @return void
         */
        function unique() {
            $user_id = $this->input->post('user_id', TRUE);
            $field = $this->input->post('field', TRUE);

            $where = array(
                $field => $this->input->post($field, TRUE),
            );

            if (!empty($user_id)) {
                $where['user_id !='] = $user_id;
            }

            $valid = ($this->User_model->exists($where)) ? FALSE : TRUE;
            echo json_encode($valid);
        }
}	
/* End of file profile.php */
/* Location: ./system/application/controllers/profile.php */
